package io.github.jast90.internship.controller;

import io.github.jast90.common.form.CommonPageRequestBody;
import io.github.jast90.internship.entity.User;
import io.github.jast90.internship.form.UserChangePasswordForm;
import io.github.jast90.internship.service.UserService;
import io.github.jast90.mybatis.pagination.domain.Page;
import io.github.jast90.mybatis.pagination.domain.PageRequest;
import io.github.jast90.web.common.controller.BaseController;
import io.github.jast90.web.common.result.CommonResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by jast90 on 2021/3/8
 */
@RestController
public class UserRestController extends BaseController<UserService, User> {

    @PostMapping("user/list")
    @Override
    public CommonResult<List<User>> queryListByCondition(@RequestBody User user) {
        return service.queryListByCondition(user);
    }

    @PostMapping("user/page")
    @Override
    public CommonResult<Page<User>> queryPage(@RequestBody CommonPageRequestBody<PageRequest, User> requestBody) {
        return service.queryPage(requestBody.getPageRequest(),requestBody.getDomain());
    }

    @GetMapping("user/{id}")
    @Override
    public CommonResult<User> queryById(@PathVariable("id") long id) {
        return service.queryById(id);
    }

    @PostMapping("user")
    @Override
    public CommonResult<User> add(@RequestBody User entity) {
        return service.add(entity);
    }

    @PutMapping("user")
    @Override
    public CommonResult<User> update(@RequestBody User entity) {
        return service.updateById(entity);
    }

    @PutMapping("user/change/password")
    public CommonResult<User> changePassword(@RequestBody UserChangePasswordForm form) {
        return service.updatePassword(form);
    }

    @DeleteMapping("user/{id}")
    @Override
    public CommonResult<User> delete(@PathVariable("id") long id) {
        return service.deleteById(id);
    }
}
